<?php


namespace app\adminapi\controller\room;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\room\department\RoomDepartmentLists;
use app\adminapi\lists\room\department\RoomDepartmentLogLists;
use app\adminapi\logic\room\department\RoomDepartmentLogic;
use app\adminapi\validate\room\department\RoomDepartmentValidate;
use app\common\enum\YesNoEnum;
use app\common\model\Building;
use app\common\model\Campus;
use app\common\model\room\department\RoomDepartment;

/**
 * RoomDepartment控制器
 * Class RoomDepartmentController
 * @package app\adminapi\controller
 */
class RoomDepartmentController extends BaseAdminController {


    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function lists() {
        return $this->dataLists(new RoomDepartmentLists());
    }


    public function logLists() {
        return $this->dataLists(new RoomDepartmentLogLists());
    }

    /**
     * 打印查询-科室汇总
     */
    public function print_lists_room_stat() {
        return $this->success('', [
            "lists" => (new RoomDepartmentLogLists())->print_lists(),
            "keyword" => (new RoomDepartment())->getPrintKeywordListRoomStat(),
        ], 1, 1);
    }

    /**
     * 打印查询-科室详情
     */
    public function print_lists_room() {
        return $this->success('', [
            "lists" => (new RoomDepartmentLogLists())->print_lists(),
            "keyword" => (new RoomDepartment())->getPrintKeywordListRoom(),
        ], 1, 1);
    }

    /**
     * 导出-科室汇总
     */
    public function export_lists_room_stat() {
        $room_lists = new RoomDepartmentLogLists();
        if ($this->request->param("export") == 2) {
            $fields = [
                'code' => '科室代码',
                'name' => '科室名',
                'room_d_use_total' => '用电度数',
                'room_d_total_fee' => '电费',
                'room_s_use_total' => '用水度数',
                'room_s_total_fee' => '水费',
                'log_date' => '时间',
            ];
            $room_lists->setExport('科室汇总', $room_lists->print_lists(), $fields);
        } else {
            $room_lists->setExport('科室汇总', [], []);
        }
        return $this->dataLists($room_lists);
    }

    /**
     * 导出-科室详情
     */
    public function export_lists_room() {
        $room_lists = new RoomDepartmentLogLists();
        if ($this->request->param("export") == 2) {
            $fields = [
                'code' => '代码',
                'name' => '科室',
                'room_s_use_total' => '用水度数',
                'room_s_price' => '水单价',
                'room_s_total_fee' => '水费',
                'room_d_use_total' => '用电度数',
                'room_d_price' => '电单价',
                'room_d_total_fee' => '电费',
                'log_date' => '时间',
            ];
            $room_lists->setExport("科室详细", $room_lists->print_lists(), $fields);
        } else {
            $room_lists->setExport('科室详细', [], []);
        }
        return $this->dataLists($room_lists);
    }

    /**
     * @notes 添加
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function add() {
        $params = (new RoomDepartmentValidate())->post()->goCheck('add');
//        if (empty($params['building'])) {
//            return $this->fail('请选择院区楼栋');
//        }
//        if (empty($params['department'])) {
//            return $this->fail('请选择科室代码');
//        }
//        $params['cid'] = $params['building'][0];
//        $params['bid'] = $params['building'][1];
//        $params['department_id'] = $params['department'][1];
        $result = RoomDepartmentLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentLogic::getError());
    }


    /**
     * @notes 编辑
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function edit() {
        $params = (new RoomDepartmentValidate())->post()->goCheck('edit');
//        if (empty($params['building'])) {
//            return $this->fail('请选择院区楼栋');
//        }
//        $params['cid'] = $params['building'][0];
//        $params['bid'] = $params['building'][1];
//        $params['department_id'] = $params['department'][1];
        $result = RoomDepartmentLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentLogic::getError());
    }


    /**
     * @notes 删除
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function delete() {
        $params = (new RoomDepartmentValidate())->post()->goCheck('delete');
        RoomDepartmentLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function detail() {
        $params = (new RoomDepartmentValidate())->goCheck('detail');
        $result = RoomDepartmentLogic::detail($params);
        return $this->data($result);
    }


    public function updateStatus() {
        $params = input();
        $result = RoomDepartmentLogic::updateStatus($params);
        if (true === $result) {
            return $this->success('修改成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentLogic::getError());
    }


    public function searchLists() {
        $month_list = [];
        $max_y = date("Y");
        $max_m = 12;
        for ($y = 2024; $y <= $max_y; $y++) {
            if ($y == $max_y) {
                $max_m = date("m");
            }
            for ($m = 1; $m <= $max_m; $m++) {
                if ($m < 10) {
                    $month_list[$y . 0 . $m] = [
                        "name" => $y . '年' . 0 . $m . '月',
                        "id" => $y . 0 . $m,
                    ];
                } else {
                    $month_list[$y . $m] = [
                        "name" => $y . '年' . $m . '月',
                        "id" => $y . $m,
                    ];
                }
            }
        }

        $cid = $this->adminCid;
        $c_where = [];
        $c_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $c_where[] = ["id", "=", $cid];
        }
        $b_where = [];
        $b_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $b_where[] = ["cid", "=", $cid];
        }

        return $this->success('', [
            "month_list" => array_reverse($month_list),
            "campus_list" => Campus::where($c_where)->select()->toArray(),
            "building_list" => Building::where($b_where)->select()->toArray(),
            "cb_lists" => Campus::field([
                "id", "name",
                "id as value",
                "name as label",
            ])->where($c_where)->with(["building" => function ($q) use ($b_where) {
                $q->field([
                    "id", "name", "cid", "id as value", "name as label",
                ])->where($b_where);
            }])->select()->each(function ($item) {
                $item->children = $item['building'];
                unset($item['building']);
                return $item;
            })->toArray(),
        ], 1, 1);
    }


    /**
     * 全部楼
     *
     * @return void
     */
    public function building() {
        $cid = $this->adminCid;

        $c_where = [];
        $c_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $c_where[] = ["id", "=", $cid];
        }

        $b_where = [];
        $b_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $b_where[] = ["cid", "=", $cid];
        }

        $result = [];
        $campus = Campus::where('status', YesNoEnum::YES)->where($c_where)->with([
            'building' => function ($query) use ($b_where) {
                $query->where($b_where);
            },
        ])->order('id', 'ASC')->select()->toArray();
        foreach ($campus as $item) {
            $children = [];
            if (!empty($item['building'])) {
                foreach ($item['building'] as $building) {
                    $children[] = [
                        'value' => $building['id'],
                        'label' => $building['name'],
                        'children' => []
                    ];
                }
            }
            $result[] = [
                'value' => $item['id'],
                'label' => $item['name'],
                'children' => $children
            ];
        }
        return $this->data($result);
    }


    /**
     * 全部科室
     *
     * @return void
     */
    public function department() {
        $cid = $this->adminCid;
        $c_where = [];
        $c_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $c_where[] = ["id", "=", $cid];
        }
        $b_where = [];
        $b_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $b_where[] = ["cid", "=", $cid];
        }

        $result = [];
        $campus = Campus::where('status', YesNoEnum::YES)->where($c_where)
            ->with('department')->order('id', 'ASC')
            ->select()->toArray();
        foreach ($campus as $item) {
            $children = [];
            if (!empty($item['department'])) {
                foreach ($item['department'] as $department) {
                    $children[] = [
                        'value' => $department['id'],
                        'label' => $department['name'],
                        'children' => []
                    ];
                }
            }
            $result[] = [
                'value' => $item['id'],
                'label' => $item['name'],
                'children' => $children
            ];
        }
        return $this->data($result);
    }


}

